
const path = require('path');
const webpack = require('webpack');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const isDev = process.env.NODE_ENV === 'development';  //在package.json中cross-env所设置的全局变量  存在于process.env中

const config = {
    target: "web",
    mode: isDev ? 'development' : 'production',   //webpack4需要mode否则起服务报warning
    entry: path.join(__dirname, 'src/index.js'),
    output: {   //输出文件
        filename: 'bundle.js',   //输出文件
        path: path.join(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /\.vue$/,   //以vue结尾的文件,webpack3  正则
                loader: 'vue-loader'
            },
            {
                test: /\.jsx$/, 
                loader: 'babel-loader'
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.styl$/,
                use: [
                    'style-loader',
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            sourceMap: true
                        }
                    },
                    'stylus-loader'
                ]
            },
            {
                test: /\.(gif|jpg|jpeg|png|svg)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 1024,
                            name: '[name]-aaa.[ext]'
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        //js代码中可用process.env.NODE_ENV判断环境   如后台api地址配置 
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: isDev ? '"development"' : '"production"'
            }
        }),
        new VueLoaderPlugin(),   //vue-loader
        new HtmlWebpackPlugin()
    ]
}

//根据

if (isDev) {
    config.devtool = '#cheap-module-eval-source-map';
    config.devServer = {
        port: '3033',   //server端口,
        host: '0.0.0.0',
        overlay: {
            errors: true,    //webpack编译错误打印至浏览器
        },
        hot: true,  //只重新渲染当前修改组件的内容

        }
    config.plugins.push(
        new webpack.HotModuleReplacementPlugin(), 
        new webpack.NoEmitOnErrorsPlugin()
    )

    }


module.exports = config;